//
//  MainTypesListViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/11.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "MainTypesListViewModel.h"
#import "MainTypesListTableViewCell.h"
#import "InputCommentViewModel.h"
#import "MainTypesListHeaderView.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "MainTypesListTopTableViewCell.h"

@interface MainTypesListViewModel ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *selfTableView;
    NSMutableArray *dataArr;
    InputCommentViewModel *boViewModel;
    MainTypesListModel *model;
}

@end

@implementation MainTypesListViewModel
- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        
        dataArr = [NSMutableArray arrayWithObjects:@" ", @"2", @"", nil];
        selfTableView = tableView;
        selfTableView.delegate = self;
        selfTableView.dataSource = self;
        selfTableView.backgroundColor = [UIColor whiteColor];
        selfTableView.mj_footer.hidden = YES;
        selfTableView.mj_header.hidden = YES;
        selfTableView.estimatedRowHeight = 40;
        selfTableView.rowHeight = UITableViewAutomaticDimension;
        [selfTableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        [selfTableView registerClass:[MainTypesListHeaderView class] forHeaderFooterViewReuseIdentifier:@"MainTypesListHeaderView"];
        [superView addSubview:selfTableView];
        [self requestCategoryList];
    }
    return self;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kHeightProportion_In_iPhoneTriploid(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NoDataFootTableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footer;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (model) {
       return model.data.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainTypesListTableViewCell *cell = [MainTypesListTableViewCell cellWithTableView:tableView];
    if (model) {
        MainTypesListArrModel *deModel = model.data[indexPath.row];
        cell.leftStr = deModel.categoryName;
    }
    cell.hidenRighImgV = YES;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.BlockSelectRow) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        MainTypesListArrModel *deModel = model.data[indexPath.row];
        dic[@"one"] = deModel;
        if (self.dic) {
            MainTypesListArrModel *model = self.dic[@"one"];
            if ([deModel.categoryCode intValue] == [model.categoryCode intValue]) {
                dic = [NSMutableDictionary dictionaryWithDictionary:self.dic];
            }
        }
        self.BlockSelectRow(dic);
    }
}


#pragma mark - 请求接口
- (void)requestCategoryList
{
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kCategorys] lodingView:nil lodingTextString:@"" isLoding:YES parameters:@{} success:^(id  _Nullable response) {
        NSDictionary * dic = response;
        self->model = [MainTypesListModel yy_modelWithJSON:dic];
        [self->selfTableView reloadData];
    } failure:^(NSError * _Nullable error) {
        DLog(@"分类=%@",error);
    }];
}

@end
